﻿package com.ustcinfo.study.hbase.r1.panwentao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.hadoop.hbase.client.Put;


/**
 * Describe:链接mysql 读取数据并存储到hbase
 * Author: panwentao 
 * Date: 2018/8/2
 */

/* 获取数据库链接 */
public class MySqlOperation {
	public static Connection getConn() {
	 Connection conn=null;
	 String URL="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8";
	 String USER="root";
	 String PASSWORD="123456";
	 try {
		Class.forName("com.mysql.jdbc.Driver");
		conn=DriverManager.getConnection(URL, USER, PASSWORD);
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 return conn;
	
	}
	
	/* 查询数据库 将数据存储到hbase */
	public static void copy(){
		Connection conn=MySqlOperation.getConn();
		PreparedStatement pst=null;
		 String s="select * from transfer_task_to";
		 try {
			 pst= conn.prepareStatement(s);
			ResultSet rs=pst.executeQuery();
			while(rs.next()) {
				Put put=new Put("id".getBytes());		
				String id=rs.getString("id");
				String task_id=rs.getString("task_id");
				String create_time=rs.getString("create_time");
				String start_time=rs.getString("start_time");
				String out_filter_number=rs.getString("out_filter_number");
				String from_run_param=rs.getString("from_run_param");
				String current_retry_times=rs.getString("current_retry_times");
				String retry_flag=rs.getString("retry_flag");
				String parent_task_id=rs.getString("parent_task_id");
				String end_time=rs.getString("end_time");
				String kill_time=rs.getString("kill_time");
				String failure_time=rs.getString("failure_time");
				
				if(id!=null) {
					put.addColumn("F1".getBytes(),"id".getBytes(), id.getBytes());
				}
                
                if(task_id!=null) {
                	put.addColumn("F1".getBytes(), "task_id".getBytes(),task_id.getBytes());
                }
                
                if(create_time!=null) {
                	 put.addColumn("F1".getBytes(), "create_time".getBytes(),create_time.getBytes());
                }
               
                if(start_time!=null) {
                	put.addColumn("F1".getBytes(), "start_time".getBytes(),start_time.getBytes());
                }
                if(out_filter_number!=null) {
                	put.addColumn("F1".getBytes(), "out_filter_number".getBytes(),out_filter_number.getBytes());
                }
                if(from_run_param!=null) {
                	 put.addColumn("F1".getBytes(), "from_run_param".getBytes(),from_run_param.getBytes());
                }
                if(current_retry_times!=null) {
                	put.addColumn("F2".getBytes(), "current_retry_times".getBytes(),current_retry_times.getBytes());
                }
                if(retry_flag!=null) {
                	put.addColumn("F2".getBytes(), "retry_flag".getBytes(),retry_flag.getBytes());
                }
                
                if(parent_task_id!=null) {
                	put.addColumn("F2".getBytes(), "parent_task_id".getBytes(),parent_task_id.getBytes());
                }
                if(end_time!=null) {
                	put.addColumn("F2".getBytes(), "end_time".getBytes(),end_time.getBytes());
                }
                if(kill_time!=null) {
                	 put.addColumn("F2".getBytes(), "kill_time".getBytes(),kill_time.getBytes());
                }
                if(failure_time!=null) {
                	put.addColumn("F2".getBytes(), "failure_time".getBytes(),failure_time.getBytes());
                }
                
                
                HBaseOperationDao.put("BD_TEST",put); //将一条数据插入hbash
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(pst!=null) {
					try {
						pst.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

		
	}
	

	
	public static void main(String[] args) {
		System.out.println(MySqlOperation.getConn());	
	}

}
